/**
 * Created by jackliu on 15/12/4.
 */
var CONFIG = require('../../common/config');

var cacheManager = require('cache-manager');
var redisStore = require('cache-manager-redis');
var redisCache = cacheManager.caching({
    store: redisStore,
    host: CONFIG.REDIS_SERVER_DOMAIN, // default value
    port: CONFIG.REDIS_SERVER_PORT, // default value
    auth_pass: CONFIG.REDIS_SERVER_PASSWORD, //
    db: CONFIG.REDIS_SERVER_DB_NAME,
    ttl: CONFIG.REDIS_SERVER_TTL
});
//------------------------------------------------------------------

var WechatAPI = require('wechat-api');
var OAuth = require('wechat-oauth');

// 微信网站应用
var config_wx_web = {
    token: '',
    appid: 'wx3d505dd750d82e65',//'wx20927d0e8f52ffff',//'wx56b59a5dcee6393a',
    appsecret: 'd5952b0a13420b7306d495e3b821bc4c',//'c0038158859fca987d9230daf69c9868',//'b93715758ee2f61fd89fd4989c2f3484',
    encodingAESKey: '',
    state: 'STATE',
};

var Wechat_Web = (function(){
    function Wechat_Web(){
        //
    }

    // 创建WechatAPI
    var KEY_PRE_API_TOKEN = 'ITB_WX_WEB_API_TOKEN_';
    var wx_api = new WechatAPI(config_wx_web.appid, config_wx_web.appsecret, function (callback) {
        // 传入一个获取全局token的方法
        redisCache.get(KEY_PRE_API_TOKEN+config_wx_web.appid, function(error, result){
            console.log("=====> [Wechat_Web] get AccessToken from redis: "+JSON.stringify(result));
            if(error || result==null){
                // 返回错误
                callback(error);
            }else{
                // 有缓存就直接返回
                callback(null,result);
            }
        });
    }, function (token, callback) {
        // 请将token存储到全局，跨进程、跨机器级别的全局，比如写到数据库、redis等
        redisCache.set(KEY_PRE_API_TOKEN+config_wx_web.appid, token);
        console.log("=====> [Wechat_Web] set AccessToken to redis!");
        callback(null, token);
    });

    // 设置Ticket(js-sdk)获取和保存的自定义方法
    var KEY_PRE_TICKET = 'ITB_WX_WEB_API_TICKETTOKEN_';
    wx_api.registerTicketHandle(function(type, callback){// Get
        redisCache.get(KEY_PRE_TICKET+type, function(err, result){
            console.log("=====> [Wechat_Web] get Wx_TicketToken("+ type +") from redis: "+JSON.stringify(result));
            if(err || result==null){
                callback(err);
            }else{
                callback(null,result);
            }
        });
    }, function(type, ticketToken, callback){// Set
        redisCache.set(KEY_PRE_TICKET+type, ticketToken);
        console.log("=====> [Wechat_Web] set Wx_TicketToken("+ type +") to redis!");
        callback(null);
    });

    // 创建OAuth
    var KEY_PRE_OAUTH = 'ITB_WX_WEB_OAUTH_';
    var wx_oauth = new OAuth(config_wx_web.appid, config_wx_web.appsecret, function (openid, callback) {
        // 传入一个获取全局token的方法
        redisCache.get(KEY_PRE_OAUTH+openid, function(error, result){
            console.log("=====> [Wechat_Web] get OpenId("+openid+") Token from redis: "+JSON.stringify(result));
            if(error || result==null){
                // 返回错误
                callback(error);
            }else{
                // 有缓存就直接返回
                callback(null,result);
            }
        });
    }, function (openid, token, callback) {
        // 请将token存储到全局，跨进程、跨机器级别的全局，比如写到数据库、redis等
        redisCache.set(KEY_PRE_OAUTH+openid, token);
        console.log("=====> [Wechat_Web] set OpenId("+openid+") Token to redis!");
        callback(null, token);
    });


    // 返回WechatAPI
    Wechat_Web.getWxApi = function() {
        return wx_api;
    }
    // 返回OAuth
    Wechat_Web.getWxOAuth = function() {
        return wx_oauth;
    }

    return Wechat_Web;
})();
//------------------------------------------------------------------

exports.Wechat_Test = (function(){
    function Wechat_Test(){
        //
    }

    var config_wx_test = {
        token: '',
        appid: 'wx39a2e5b8aede9ddf',
        appsecret: 'e366a4fbacb5c6478046f31729589329',
        encodingAESKey: '',
        state: 'STATE',
    };

    // 创建WechatAPI
    var KEY_PRE_API_TOKEN = 'ITB_WX_TEST_API_TOKEN_';
    var wx_api = new WechatAPI(config_wx_test.appid, config_wx_test.appsecret, function (callback) {
        // 传入一个获取全局token的方法
        redisCache.get(KEY_PRE_API_TOKEN+config_wx_test.appid, function(error, result){
            console.log("=====> [Wechat_Test] get AccessToken from redis: "+JSON.stringify(result));
            if(error || result==null){
                // 返回错误
                callback(error);
            }else{
                // 有缓存就直接返回
                callback(null,result);
            }
        });
    }, function (token, callback) {
        // 请将token存储到全局，跨进程、跨机器级别的全局，比如写到数据库、redis等
        redisCache.set(KEY_PRE_API_TOKEN+config_wx_test.appid, token);
        console.log("=====> [Wechat_Test] set AccessToken to redis!");
        callback(null, token);
    });

    // 设置Ticket(js-sdk)获取和保存的自定义方法
    var KEY_PRE_TICKET = 'ITB_WX_TEST_API_TICKETTOKEN_';
    wx_api.registerTicketHandle(function(type, callback){// Get
        redisCache.get(KEY_PRE_TICKET+type, function(err, result){
            console.log("=====> [Wechat_Test] get Wx_TicketToken("+ type +") from redis: "+JSON.stringify(result));
            if(err || result==null){
                callback(err);
            }else{
                callback(null,result);
            }
        });
    }, function(type, ticketToken, callback){// Set
        redisCache.set(KEY_PRE_TICKET+type, ticketToken);
        console.log("=====> [Wechat_Test] set Wx_TicketToken("+ type +") to redis!");
        callback(null);
    });

    // 创建OAuth
    var KEY_PRE_OAUTH = 'ITB_WX_TEST_OAUTH_';
    var wx_oauth = new OAuth(config_wx_test.appid, config_wx_test.appsecret, function (openid, callback) {
        // 传入一个获取全局token的方法
        redisCache.get(KEY_PRE_OAUTH+openid, function(error, result){
            console.log("=====> [Wechat_Test] get OpenId("+openid+") Token from redis: "+JSON.stringify(result));
            if(error || result==null){
                // 返回错误
                callback(error);
            }else{
                // 有缓存就直接返回
                callback(null,result);
            }
        });
    }, function (openid, token, callback) {
        // 请将token存储到全局，跨进程、跨机器级别的全局，比如写到数据库、redis等
        redisCache.set(KEY_PRE_OAUTH+openid, token);
        console.log("=====> [Wechat_Test] set OpenId("+openid+") Token to redis!");
        callback(null, token);
    });


    // 返回WechatAPI
    Wechat_Test.getWxApi = function() {
        return wx_api;
    }
    // 返回OAuth
    Wechat_Test.getWxOAuth = function() {
        return wx_oauth;
    }

    return Wechat_Test;
})();
//------------------------------------------------------------------

exports.getWxWeb = function(){
    return Wechat_Web;
}



